<?php if (!defined('THINK_PATH')) exit();?><!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<base href="/tpshop/" />
	<title>京东(JD.COM)-综合网购首选-正品低价、品质保障、配送及时、轻松购物！</title>
	<link rel="stylesheet" href="/tpshop/Public/css/bootstrap.min.css">
	<link rel="stylesheet" href="/tpshop/Public/css/bootstrap-theme.min.css">
	<link rel="stylesheet" href="/tpshop/Public/css/front.css">
	<link rel="stylesheet" href="/tpshop/Public/css/nivo-slider.css">
	
</head>
<body>
	<div class="container">
		<form action="<?=U('shopping/order')?>" method="post">
			<div class="row" id="header" >
				<div class="col-md-3">
					<a href="/tpshop/index.php"><img id="logo" src="/tpshop/Public/img/logo.png" /></a>
				</div>
				<div class="col-sm-9">
				    <img height="50" class="pull-right" src="/tpshop/Public/img/shop_step.jpg?>" />
				</div>
			</div>
			<div class="row">
				<div class="col-md-12">
					<table class="table table-bordered">
						<tr>
							<th>商品</th>
							<th width="160">价格</th>
							<th width="140">数量</th>
							<th width="140">小计</th>
							<th width="100">操作</th>
						</tr>
						<?php
 $sum = 0; foreach($carts as $c){ $product = M('product')->where('id ='.$c['product_id'])->find(); ?>
						<tr class="t">
							<td><input class="is_select" value="<?=$c['id']?>" type="checkbox"><a href="<?=U('product/detail/'.$product['id'])?>">
							<input class="product_id" name="product_id" type="hidden" value="<?=$c['product_id']?>">
							<img style="width:60px;" src="/tpshop<?=$product['img']?>"><?=$product['title']?></a></td>
							<td>￥<?=$product['price']?></td>
							<td class="cart_num">
								<span class="minus">-</span>
								<input name="num" type="text" class="n" value="<?=$c['num']?>">
								<span class="add">+</span>
							</td>
							<td>￥<?php echo $p = intval($product['price']) * intval($c['num'])?></td>
							<?php $sum = $sum + $p;?>
							<td><a href="<?=U('home/shopping/cartdel/id/'.$c['id'])?>">删除</a></td>
						</tr>
						<?php }?>
						<tr>
							<td colspan="10">
								<div class="checkbox">
									<label>
										<input type="checkbox" class="checkall">全选
									</label>
								&nbsp;&nbsp;&nbsp;&nbsp;<a id="delete_all" href="#">删除选中的产品</a>
								<p style="float:right;" class="num">合计：￥<?=$sum?></p>
								</div>
							</td>
						</tr>
					</table>
						<button id="checkout"  class="btn btn-danger pull-right">去结算</button>
				</div>
			</div>
		</form>
	</div>
	<script type="text/javascript" src="/tpshop/Public/js/jquery-3.1.0.min.js")?>"></script>
	<script type="text/javascript">
		$(function(){
			$(".checkall").click(function(){
				$(".is_select").prop("checked",this.checked);
			});
			$('#delete_all').click(function(){
				var ids = [];
				$(".is_select").each(function(i,item){
					if($(item).prop('checked')){
						ids.push($(item).val());
					}
				});
				if(ids.length == 0){

				}else{
					$.post("<?=U('home/shopping/cartdels')?>",{'ids' : ids},function(data){
						if(data == 'ok'){
							location.reload();
						}
					})
				}
				return false;
			});	
			$(".minus").click(function(){
				var id = $(this).parents('.t').find('.is_select').val();
				var num = parseInt($(this).parents('.cart_num').find('.n').val()) - 1;
				$.get('<?=U('home/shopping/updateCart')?>',{'id':id,'num':num},function(data){
					if(data == 'ok'){
						location.reload();
					}else{
						alert(data);
					}
				})
			})		
			$(".add").click(function(){
				var id = $(this).parents('.t').find('.is_select').val();
				var num = parseInt($(this).parents('.cart_num').find('.n').val()) + 1;
				$.get('<?=U('home/shopping/updateCart')?>',{'id':id,'num':num},function(data){
					if(data == 'ok'){
						location.reload();
					}else{
						alert(data);
					}
				})
			})	
			$(".n").click(function(){
				var id = $(this).parents('.t').find('.is_select').val();
				var num = parseInt($(this).parents('.cart_num').find('.n').val());
				$.get('<?=U('home/shopping/updateCart')?>',{'id':id,'num':num},function(data){
					if(data == 'ok'){
						location.reload();
					}else{
						alert(data);
					}
				})
			})		
			$('#checkout').click(function(){
				var data = [];
				$(".is_select").each(function(i,item){
					if($(item).prop('checked')){
						var element = {
							'product_id' : $(this).parents('.t').find('.product_id').val(),
							'num' : $(this).parents('.t').find('.n').val()
						}
						data.push(element);
					}
				});
				$.post('<?=U('home/shopping/preOrder')?>',{'data':data},function(data){
					if (data == 'ok') {
						location.href = "<?=U('home/shopping/order')?>"
					}
				})
				return false;
			})	
		})
	</script>
</body>
</html>